package com.hangzhou.xyj.ui.activity;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.net.handler.RegistHandler;
import com.hangzhou.xyj.net.handler.SmsShortCodeHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.RegisterReq;
import com.hangzhou.xyj.net.request.SmsShortCodeReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.ToastUtil;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private Button mBtnGetCode;
    private EditText mEtTel;//手机号码
    private EditText mEtPwd;//密码
    private EditText mEtCode;//验证码
    private Button mBtnLogin;//登录按钮

    @Override
    public void initTop() {
        enableTop(true);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_register);
    }

    @Override
    public void initViews() {
        mBtnGetCode = (Button) findViewById(R.id.btn_get_code);
        mEtTel = (EditText) findViewById(R.id.et_tel);
        mEtPwd = (EditText) findViewById(R.id.et_password);
        mEtCode = (EditText) findViewById(R.id.et_short_code);
        mBtnLogin = (Button) findViewById(R.id.btn_register);
    }

    @Override
    public void initDatas() {

    }

    @Override
    public void initListeners() {
        mBtnGetCode.setOnClickListener(this);
        mBtnLogin.setOnClickListener(this);
    }


    /**
     * 校验手机号
     */
    private boolean checkPhoneNum() {
        //发送请求获取验证码
        String phoneNum = mEtTel.getText().toString().trim();
        if (StringUtil.isBlank(phoneNum)) {
            ToastUtil.show("手机号输入为空");
            return false;
        }
        //校验手机号是否合法
        if (!PublicUtil.isMobilePhone(phoneNum)) {
            ToastUtil.show("手机号输入非法，请重新输入");
            mEtTel.setText("");
            return false;
        }
        return true;
    }

    /**
     * 校验密码输入
     *
     * @return
     */
    private boolean checkPwd() {
        String password = mEtPwd.getText().toString().trim();
        if (StringUtil.isBlank(password)) {
            ToastUtil.show("密码输入为空");
            return false;
        }
        return true;
    }

    /**
     * 校验验证码
     *
     * @return
     */
    private boolean checkSmsShorCode() {
        String smsShortCode = mEtCode.getText().toString().trim();
        if (StringUtil.isBlank(smsShortCode)) {
            ToastUtil.show("验证码输入为空");
            return false;
        }
        return true;
    }

    /**
     * 获取验证码请求
     */
    private void getSmsShortCode() {
        String phoneNum = mEtTel.getText().toString().trim();
        SmsShortCodeReq req = new SmsShortCodeReq(phoneNum, SmsShortCodeReq.TYPE_REGIST);
        HttpLoader.getInstance().sendRequest(new SmsShortCodeHandler(req, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                boolean isSuccess = (boolean) model;
                if (isSuccess) {
                    ToastUtil.show("验证码已发送，注意查收！");
                }
            }
        }, 0));
        //倒计时60秒，才可以重新获取验证码
        mBtnGetCode.setEnabled(false);
        mBtnGetCode.setBackgroundResource(R.drawable.bg_gray_rect);
        mBtnGetCode.setTextColor(Color.parseColor("#636363"));
        new MyCountDownTimer(60000, 1000).start();
    }

    /**
     * 注册请求
     */
    private void register() {
        String phoneNum = mEtTel.getText().toString().trim();
        String password = mEtPwd.getText().toString().trim();
        String shortCode = mEtCode.getText().toString().trim();
        RegisterReq registerReq = new RegisterReq(phoneNum, password, shortCode);
        HttpLoader.getInstance().sendRequest(new RegistHandler(registerReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if (model != null) {
                    //登录成功，跳转到主页
                    ToastUtil.show("注册成功！");
                    IntentManager.getInstance().goLoginActivity(mContext);
                    closeActivity();
                }
            }
        }, 0));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_get_code://点击“获取校验码”按钮
                if (checkPhoneNum()) {
                    //手机号输入正确，获取验证码
                    getSmsShortCode();
                }
                break;
            case R.id.btn_register://点击“注册”按钮
                if (checkPhoneNum() && checkPwd() && checkSmsShorCode()) {
                    register();
                }
                break;
        }
    }

    /**
     * 倒计时,60秒以后才可以重新获取验证码
     */
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            mBtnGetCode.setEnabled(true);
            mBtnGetCode.setTextColor(Color.parseColor("#ffffff"));
            mBtnGetCode.setBackgroundResource(R.drawable.bg_round_rect);
            mBtnGetCode.setText("获取验证码");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mBtnGetCode.setText(millisUntilFinished / 1000 + "秒重新获取");
        }
    }
}
